package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.ShopCar;
import cn.wolfcode.trip.base.service.IIntegralService;
import cn.wolfcode.trip.base.service.IShopCarService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//积分资源控制器
@RestController
@RequestMapping("/shopCar")
public class ShopCarController {
    @Autowired
    private IShopCarService shopCarService;

    /**
     * 将数据写入和购物车表
     * 资源:/shopCar
     * 动作:Post
     */
    @PostMapping
    public Object save(ShopCar entity){
        JSONResult result = new JSONResult();
        try {
            //调用业务层注册的方法
            shopCarService.save(entity);
            result.setData(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;

    }
    /**
     * 根据用户id查该用户的购物车
     * 资源:/shopCar
     * 动作:Get
     * 参数:userId
     */
    @GetMapping("/{userId}")
    public Object getProductByUser(@PathVariable Long userId){


        return shopCarService.getProductByUser(userId);
    }

    /**
     * 根据购物车id删除该商品
     * 资源:/shopCar
     * 动作:Get
     * 参数:userId
     */
    @DeleteMapping("/{id}")
    public Object delete(@PathVariable Long id){
        JSONResult result = new JSONResult();
        try {
            //调用业务层注册的方法
            shopCarService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;

    }
}
